
package com.hlkj.pay.util;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

import org.springframework.util.StringUtils;

import com.hlkj.pay.common.constants.MerchantConstant;
import com.hlkj.pay.enums.MerchantEnum;

/**
 * @author HlpayTeam
 * @date 2024/09/12 13:34
 */
public class MerchantUtils {

    /**
     * sn 生成
     * 
     * @param sysType
     * @return
     */
    public static String sn(MerchantEnum.MERCHANT_SN_TYPE sysType) {
        return sysType.getSnPrefix() + code(9);
    }

    /**
     * sn 生成
     * 
     * @param amount
     * @return
     */
    public static String code(String prefix, int num) {
        Random random = new Random();
        String s = String.valueOf(System.currentTimeMillis());
        int tenDigitNumber = random.nextInt(3) + 1; // 生成1到999999999之间的随机数，确保有一位数是0
        String formattedNumber = String.format("%03d", tenDigitNumber); // 格式化为9位数字字符串，不足部分用0填充
        return prefix + s.substring(s.length() - num + 3) + formattedNumber;
    }

    /**
     * sn 生成
     * 
     * @param amount
     * @return
     */
    public static String code(int num) {
        Random random = new Random();
        String s = String.valueOf(System.currentTimeMillis());
        int tenDigitNumber = random.nextInt(3) + 1; // 生成1到999999999之间的随机数，确保有一位数是0
        String formattedNumber = String.format("%03d", tenDigitNumber); // 格式化为9位数字字符串，不足部分用0填充
        return s.substring(s.length() - num + 3) + formattedNumber;
    }

    /**
     * sn 生成
     * 
     * @param amount
     * @return
     */
    public static BigDecimal calculateFeeUp(BigDecimal amount, BigDecimal commRateBase) {
        return commRateBase.multiply(amount).divide(new BigDecimal(MerchantConstant.REDIS_SN_FEE_AMOUNT_BASE), 4, RoundingMode.HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * sn 生成
     * 
     * @param amount
     * @return
     */
    public static BigDecimal doubleToBigDecimal(Double amount) {
        return new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * sn 生成
     *
     * @param amount
     * @return
     */
    public static BigDecimal stringToBigDecimal(String amount) {
        if (!StringUtils.hasText(amount)) {
            return null;
        }
        return new BigDecimal(amount).setScale(4, BigDecimal.ROUND_HALF_UP);
    }
    /**
     * sn 生成
     *
     * @param amount
     * @return
     */
    public static Integer stringToFen(String amount) {
        if (!StringUtils.hasText(amount)) {
            return null;
        }
        return new BigDecimal(amount).multiply(new BigDecimal(100)).intValue();
    }


    /**
     * sn 生成
     * 
     * @param amount
     * @return
     */
    public static String bigDecimalToString(BigDecimal amount) {
        if (amount == null) {
            return "0.0000";
        }
        return amount.setScale(4, RoundingMode.HALF_UP).toString();
    }

    /**
     * sn 生成
     *
     * @param amount
     * @return
     */
    public static String bigDecimalToString2(BigDecimal amount) {
        if (amount == null) {
            return "0.00";
        }
        return amount.setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 除法运算
     * 
     * @param amount
     * @return
     */
    public static BigDecimal divide(BigDecimal amount, BigDecimal divisor) {
        if (amount == null || divisor == null || amount.compareTo(BigDecimal.ZERO) == 0 || divisor.compareTo(BigDecimal.ZERO) == 0) {
            return new BigDecimal(0);
        }
        return amount.divide(divisor, 4, RoundingMode.HALF_UP).setScale(4);
    }

    /**
     * 乘法运算
     *
     * @param amount
     * @return
     */
    public static BigDecimal multiply(BigDecimal amount, BigDecimal multiply) {
        if (amount == null || multiply == null) {
            return new BigDecimal(0);
        }
        return amount.multiply(multiply).setScale(4, BigDecimal.ROUND_HALF_DOWN);
    }

    /**
     * 减法运算
     *
     * @param amount
     * @return
     */
    public static BigDecimal subtract(BigDecimal amount, BigDecimal subtract) {
        if (amount == null || subtract == null) {
            return new BigDecimal(0);
        }
        return amount.subtract(subtract).setScale(4, BigDecimal.ROUND_HALF_DOWN);
    }

    /**
     * 加法运算
     *
     * @param amount
     * @return
     */
    public static BigDecimal add(BigDecimal amount, BigDecimal subtract) {
        if (amount == null || subtract == null) {
            return new BigDecimal(0);
        }
        return amount.add(subtract).setScale(4, BigDecimal.ROUND_HALF_DOWN);
    }

    /**
     * sn 生成
     *
     * @param amount
     * @return
     */
    public static BigDecimal fenToYuan(Long amount) {
        if (amount == null) {
            return new BigDecimal(0);
        }
        return divide(new BigDecimal(amount), new BigDecimal(100));
    }

    /**
     * sn 生成
     *
     * @param amount
     * @return
     */
    public static BigDecimal fenToYuan(String amount) {
        if (!StringUtils.hasText(amount)) {
            return new BigDecimal(0);
        }
        return divide(new BigDecimal(amount), new BigDecimal(100));
    }

    /**
     * sn 生成
     *
     * @param amount
     * @return
     */
    public static BigDecimal fenToYuan(Integer amount) {
        if (amount == null) {
            return new BigDecimal(0);
        }
        return divide(new BigDecimal(amount), new BigDecimal(100));
    }

    public static Integer YuanToFen(BigDecimal amount) {
        if (amount == null) {
            return 0;
        }
        return amount.multiply(new BigDecimal(100)).intValue();
    }
}
